<template>
	<view class="container">
		<view class="model-list">
			<view class="model-item" v-for="(item,index) in modelList" :key="index" @click="modelClick(item)">
				<text class="name">{{item.name}}</text>
				<text class="price" v-if="item.guide_price">{{item.guide_price}}万</text>
			</view>
		</view>
		<view class="custom-model-txt" v-if="modelList.length>0" @click="customModelClick">自定义车型</view>
		<!-- 自定义车型 -->
		<uni-popup ref="popup" type="bottom">
			<view class="custom-model">
				<view class="c-title">自定义车型</view>
				<view class="c-content">
					<input :adjust-position="false" @blur="keyboardHeight=0" :cursorSpacing="20" type="text" :focus="inputFocus" placeholder="请输入车型" v-model="customModel">
					<view class="confirm-btn" @click="confirm">确定</view>
				</view>
			</view>
			<view :style="{height:keyboardHeight+'px'}"></view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		vehicleModelList
	} from '@/api/source.js'
	export default {
		data() {
			return {
				keyboardHeight:0,
				reLaunch:false,
				inputFocus: false,
				modelList: [],
				customModel: "",
				source_type_id: '',
				brand_id: '',
				series_id: '',
			}
		},
		onLoad(option) {
			this.source_type_id = option.source_type_id || '';
			this.brand_id = option.brand_id || '';
			this.series_id = option.series_id || '';
			this.init();
		},
		onShow() {
			// if(this.reLaunch){
			// 	uni.reLaunch({
			// 		url:'/main/carSourceList/releaseCar'
			// 	})
			// }this._getList(true);
			uni.onKeyboardHeightChange(res => {
				this.keyboardHeight=res.height
			})
		},
		onUnload() {
			uni.offKeyboardHeightChange();
		},
		methods: {
			init() {
				vehicleModelList({
					series_id: this.series_id,
					source_type_id: this.source_type_id
				}).then(res => {
					this.modelList = res.data
					if(this.modelList.length>0){
						this.customModel =  this.modelList[0].custom_name || ''
					}
					
				})
			},
			modelClick(item) {
				this.setBack(item)
			},
			// 自定义车型
			customModelClick() {
				this.$refs.popup.open()
				this.inputFocus = true
			},
			confirm() {
				if (this.customModel) {
					let first=this.modelList[0];
					let data={
						guide_price:'',
						name:this.customModel,
						source_type_id:first.source_type_id,
						brand_id:first.brand_id,
						series_one_id:first.series_one_id,
						series_two_id:first.series_two_id,
						source_type_name:first.source_type_name,
					}
					data.name=this.customModel;
					data.id='';
					data.pid='';
					data.guide_price='';
					this.setBack(data)
				} else {
					uni.showToast({
						title: '请输入自定义车型',
						icon: 'none'
					})
				}

			},
			setBack(data) {
				this.reLaunch=true;
				uni.navigateTo({
					url:'/pages/carSource/push/single?car='+JSON.stringify(data)
				})
				return;
				var pages = getCurrentPages()
				let num = 0;
				const prevPage = pages.find((page, index) => {
					if (page.route == 'pages/carSource/push/single') {
						num = index;
						return true;
					}
				})
				// console.log(data);
				var delta = pages.length - num - 1;
				prevPage.$vm.backData({
					type: 'model',
					data
				})
				// 返回上一页
				uni.navigateBack({
					delta: delta
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		// background-color: #F7F8FA;
		padding-top: 20rpx;
	}

	.model-list {
		.model-item {
			background-color: #fff;
			padding: 29rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			border-bottom: 1rpx solid #E6E6E6;

			&:last-child {
				// border-bottom: none;
			}

			.name {
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #000000;
			}

			.price {
				font-size: 24rpx;
				font-family: PingFang SC;
				font-weight: 400;
				color: #999999;
				white-space: nowrap;
			}
		}
	}

	.custom-model-txt {
		background-color: #fff;
		padding: 29rpx;
		font-size: 28rpx;
		font-family: PingFang SC;
		font-weight: 500;
		color: #000000;
	}

	.custom-model {
		background-color: #fff;
		padding: 40rpx 28rpx;

		.c-title {
			font-size: 32rpx;
			font-family: PingFang SC;
			font-weight: 800;
			color: #000000;
			text-align: center;
		}

		.c-content {
			margin-top: 34rpx;
			display: flex;
			align-items: center;

			input {
				padding: 0 12rpx;
				flex: 1;
				height: 92rpx;
				background: #F3F3F3;
				border-radius: 14rpx;
			}

			view {
				margin-left: 23rpx;
				width: 173rpx;
				height: 92rpx;
				line-height: 92rpx;
				background: #0E59EA;
				border-radius: 14rpx;
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #FFFFFF;
				text-align: center;
			}
		}
	}
</style>